package com.cqwshzj.util;/**
 * @Description:
 * @Auther: Huang Zhijin
 * @Version: 1.0.0
 * @Date: 2021/4/6 18:25
 */

import com.cqwshzj.framework.exception.unchecked.JobFrameworkException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.Map;

/**
 *
 * 〈〉
 *
 * @author Huang Zhijin
 * @create 2021/4/6
 * @since 1.0.0
 */
public class JobAssertUtil {


	public static void isTrue( boolean expression, String message, String jobId){
		if (!expression) {
			throw new JobFrameworkException(message,jobId);
		}
	}

	public static void isTrue( boolean expression, String jobId){
		isTrue(expression,"[Assertion failed] - this expression must be true",jobId);
	}

	public static void isNull(Object object, String message, String jobId){
		if ( object != null) {
			throw new JobFrameworkException(message,jobId);
		}
	}

	public static void isNull(Object object, String jobId){
		isNull(object,"[Assertion failed] object must be null",jobId);
	}

	public static void notNull(Object object, String message, String jobId){
		if ( object == null) {
			throw new JobFrameworkException(message,jobId);
		}
	}

	public static void notNull(Object object, String jobId){
		notNull(object,"[Assertion failed] object must not be null",jobId);
	}

	public static void hasLength(String text, String message, String jobId){
		if ( text != null && !text.isEmpty()) {
			throw new JobFrameworkException(message,jobId);
		}
	}

	public static void hasLength(String text, String jobId){
		hasLength(text,"[Assertion failed] text is empty",jobId);
	}

	public static void hasText(String text, String message, String jobId){
		if (StringUtils.isEmpty(text)) {
			throw new JobFrameworkException(message,jobId);
		}
	}

	public static void hasText(String text, String jobId){
		hasText(text,"[Assertion failed] text is empty",jobId);
	}

	public static void notEmpty(Object[] array, String message, String jobId){
		if (ArrayUtils.isEmpty(array)) {
			throw new JobFrameworkException(message,jobId);
		}
	}

	public static void notEmpty(Object[] array, String jobId){
		notEmpty(array,"[Assertion failed] text is empty", jobId);
	}

	public static void noNullElements(Object[] array, String message, String jobId){
		if (array != null) {
			for (Object element : array) {
				if (element == null) {
					throw new JobFrameworkException(message,jobId);
				}
			}
		}
	}

	public static void noNullElements(Object[] array, String jobId){
		noNullElements(array,"[Assertion failed] array has null element ",jobId);
	}

	public static void notEmpty(Collection<?> collection, String message, String jobId){
		if (CollectionUtils.isEmpty(collection)) {
			throw new JobFrameworkException(message,jobId);
		}
	}

	public static void notEmpty(Collection<?> collection, String jobId){
		notEmpty(collection,"[Assertion failed] collection is empty ",jobId);
	}

	public static void notEmpty(Map<?,?> map, String message, String jobId){
		if (MapUtils.isEmpty(map)) {
			throw new JobFrameworkException(message,jobId);
		}
	}

	public static void notEmpty(Map<?,?> map, String jobId) {
		notEmpty(map,"[Assertion failed] map is empty ",jobId);
	}

}
